package org.zn.note.jdk.thread;

import org.zn.note.common.util.ThreadUtil;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName Task
 * @Author zn
 * @Date 2019/3/28 14:31
 **/
public class Task implements Runnable {

    private String taskId;
    private static final Random RANDOM_SLEEP_TIME = new Random();

    public Task(String taskId) {
        this.taskId = taskId;
    }

    @Override
    public void run() {
        try {
            doTask(taskId);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void doTask(String taskId) throws InterruptedException {
//        doTask0(taskId, TimeUnit.HOURS, 10);
        int sleepTime = RANDOM_SLEEP_TIME.nextInt(3);
        doTask0(taskId, TimeUnit.SECONDS, sleepTime);
    }

    private void doTask0(String taskId, TimeUnit timeUnit, long speadTime) throws InterruptedException {
        ThreadUtil.printThreadTask(taskId, "doTask start.");
        // process task spead time
        timeUnit.sleep(speadTime);
        ThreadUtil.printThreadTask(taskId, "doTask finish.");
    }
}
